
(function(){
	var api = 'http://h6.duchengjiu.top/shop/';
	//获取热门商品
	var cartId = getUrlValue('cart_id');
	var positionId = 0;
	var hotContent = document.getElementById('goods-hot');
	var templateStr = document.getElementById('template').innerHTML;
	var pages=1;
	var pageCount=0;
	function getGoodsHot(pages){
		$.get(api + 'api_goods.php',{'cat_id':cartId, 'page':pages,'pagesize':9},function(data){
//			console.log(data)
			if(data.code == 1){
				hotContent.innerHTML = '客官别急，新品马上上架!';
				hotContent.className='bieji';
				return;
			};
			pageCount=data.page.page_count;
			var obj = data.data;
			var str='';
			var compile = _.template(templateStr);
			//console.log(obj);
			for(var i = 0; i < obj.length; i++){
				str += compile(obj[i]);
			}
			hotContent.innerHTML = str;
			$('[lazyloadsrc]').YdxLazyLoad();
			
			
			//分页
			$('.page').pagination({
				pageCount : pageCount, //总多少页
				//current : 1, //默认当前第几页
				activeCls : 'pageCls', //当前选中页样式
				prevContent: '上一页',
				nextContent: '下一页',
				mode : 'fixed',
				count: 4,
				coping: true,
				homePage : '首页',
				endPage : '尾页',
				jump : true,
				callback : function(apis){
					var activePage = apis.getCurrent();
					//console.log(activePage);
					//在当下请求数据
					//getGoodsHot(activePage);
					$.get(api + 'api_goods.php', {'cat_id':cartId, 'page':activePage, 'pagesize':9}, function(data){
						//console.log(data)
						if(data.code == 1){
							hotContent.innerHTML = '客官别急，新品马上上架!';
							return;
						};
						pageCount = data.page.page_count;
						var obj = data.data;
						//console.log(obj);
						//遍历出数据添加到页面
						var str ='';
						var tempile = _.template(templateStr);
						for(var i = 0; i < obj.length; i++){
							str += tempile(obj[i]);
						};
						hotContent.innerHTML = str;
						//懒加载图片调用
						$('[lazyloadsrc]').YdxLazyLoad();
					});	
				},
			});
		});
	};
	
	getGoodsHot(pages);
	
})();



//console.log(window.location)
//这个方法用来干 什么的？
function getUrlValue(name){//?goods_id=257557&age=haha
		var str = location.search.substr(1);//goods_id=257557
		var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
		var result = str.match(reg);
		//console.log(result)
		//console.log(result);
		if(result === null) return null;
		return result[2];
};